<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Toast, Dialog } from 'vant'
import api from '@/api'

const router = useRouter()

// 用户信息
const userInfo = ref({
  id: 1001,
  username: '航空爱好者',
  avatar: 'https://picsum.photos/200/200?random=1',
  level: 3,
  points: 1250,
  balance: 0.00,
  isVip: false
})

// 我的订单
const orders = reactive([
  { id: 1, icon: 'pending-payment', text: '待付款' },
  { id: 2, icon: 'logistics', text: '待发货' },
  { id: 3, icon: 'completed', text: '待收货' },
  { id: 4, icon: 'comment-o', text: '待评价' },
  { id: 5, icon: 'after-sale', text: '退款/售后' }
])

// 我的服务
const services = reactive([
  { id: 1, icon: 'coupon-o', text: '优惠券', badge: 2 },
  { id: 2, icon: 'gold-coin-o', text: '我的积分', badge: 0 },
  { id: 3, icon: 'gift-o', text: '每日签到', badge: 1 },
  { id: 4, icon: 'friends-o', text: '邀请好友', badge: 0 },
  { id: 5, icon: 'service-o', text: '客服中心', badge: 0 },
  { id: 6, icon: 'setting-o', text: '设置', badge: 0 },
  { id: 7, icon: 'question-o', text: '帮助中心', badge: 0 },
  { id: 8, icon: 'info-o', text: '关于我们', badge: 0 }
])

// 页面加载完成后初始化
onMounted(() => {
  loadUserInfo()
})

// 加载用户信息
const loadUserInfo = async () => {
  try {
    // 实际项目中应该从API获取数据
    // const response = await api.user.getUserInfo()
    // userInfo.value = response.data
    console.log('加载用户信息')
  } catch (error) {
    console.error('加载用户信息失败:', error)
  }
}

// 处理订单点击
const handleOrderClick = (order) => {
  console.log('点击订单:', order.text)
  router.push({
    path: '/orders',
    query: { type: order.id }
  })
}

// 处理服务点击
const handleServiceClick = (service) => {
  console.log('点击服务:', service.text)
  
  switch (service.text) {
    case '优惠券':
      router.push('/coupons')
      break
    case '我的积分':
      router.push('/points')
      break
    case '每日签到':
      Dialog({
        title: '每日签到',
        message: '签到成功，获得10积分！',
        confirmButtonText: '知道了'
      })
      break
    case '邀请好友':
      Toast('邀请好友功能开发中')
      break
    case '客服中心':
      Toast('客服中心功能开发中')
      break
    case '设置':
      router.push('/settings')
      break
    case '帮助中心':
      router.push('/help')
      break
    case '关于我们':
      router.push('/about')
      break
    default:
      Toast('功能开发中')
  }
}

// 处理余额点击
const handleBalanceClick = () => {
  router.push('/wallet')
}

// 处理会员点击
const handleVipClick = () => {
  if (userInfo.value.isVip) {
    router.push('/vip-center')
  } else {
    Dialog({
      title: '开通会员',
      message: '是否立即开通会员，享受更多特权？',
      confirmButtonText: '立即开通',
      cancelButtonText: '暂不开通'
    }).then(() => {
      router.push('/vip-purchase')
    }).catch(() => {
      // 用户取消
    })
  }
}

// 处理编辑资料点击
const handleEditProfileClick = () => {
  router.push('/edit-profile')
}

// 处理登出点击
const handleLogoutClick = () => {
  Dialog({
    title: '退出登录',
    message: '确定要退出登录吗？',
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(() => {
    // 实际项目中应该调用API
    // await api.user.logout()
    Toast('退出登录成功')
    router.push('/login')
  }).catch(() => {
    // 用户取消
  })
}
</script>

<template>
  <div class="page-container">
    <!-- 用户信息卡片 -->
    <div class="user-card">
      <div class="user-info">
        <div class="avatar-container">
          <img :src="userInfo.avatar" alt="用户头像" class="avatar">
          <div class="level-tag">Lv.{{ userInfo.level }}</div>
        </div>
        <div class="user-details">
          <div class="username">{{ userInfo.username }}</div>
          <div class="user-id">ID: {{ userInfo.id }}</div>
          <div class="edit-profile" @click="handleEditProfileClick">
            <van-icon name="edit" />
            <span>编辑资料</span>
          </div>
        </div>
      </div>
      
      <!-- 会员信息 -->
      <div class="vip-section" @click="handleVipClick">
        <div class="vip-info">
          <div class="vip-tag" :class="{ active: userInfo.isVip }">
            {{ userInfo.isVip ? 'VIP会员' : '开通会员' }}
          </div>
          <div class="vip-desc">
            {{ userInfo.isVip ? '尊享会员特权' : '开通会员享受更多权益' }}
          </div>
        </div>
        <van-icon name="arrow" />
      </div>
      
      <!-- 资产信息 -->
      <div class="assets">
        <div class="asset-item" @click="handleBalanceClick">
          <div class="asset-value">{{ userInfo.balance }}</div>
          <div class="asset-label">余额</div>
        </div>
        <div class="asset-item">
          <div class="asset-value">{{ userInfo.points }}</div>
          <div class="asset-label">积分</div>
        </div>
        <div class="asset-item">
          <div class="asset-value">0</div>
          <div class="asset-label">优惠券</div>
        </div>
      </div>
    </div>
    
    <!-- 我的订单 -->
    <div class="card">
      <div class="card-header">
        <div class="card-title">我的订单</div>
        <div class="card-more" @click="router.push('/orders')">
          <span>全部订单</span>
          <van-icon name="arrow" />
        </div>
      </div>
      <div class="order-icons">
        <div 
          v-for="order in orders" 
          :key="order.id" 
          class="order-icon-item"
          @click="handleOrderClick(order)"
        >
          <div class="icon-wrapper">
            <van-icon :name="order.icon" />
          </div>
          <div class="icon-text">{{ order.text }}</div>
        </div>
      </div>
    </div>
    
    <!-- 我的服务 -->
    <div class="card">
      <div class="card-header">
        <div class="card-title">我的服务</div>
      </div>
      <div class="service-grid">
        <div 
          v-for="service in services" 
          :key="service.id" 
          class="service-item"
          @click="handleServiceClick(service)"
        >
          <div class="service-icon">
            <van-icon :name="service.icon" />
            <div v-if="service.badge > 0" class="badge">{{ service.badge }}</div>
          </div>
          <div class="service-text">{{ service.text }}</div>
        </div>
      </div>
    </div>
    
    <!-- 退出登录 -->
    <div class="logout-btn" @click="handleLogoutClick">
      退出登录
    </div>
  </div>
</template>

<style scoped>
.page-container {
  background-color: #f5f7fa;
  min-height: 100vh;
  padding-bottom: 60px;
}

/* 用户信息卡片 */
.user-card {
  background-color: var(--primary-color);
  color: white;
  padding: 20px 15px;
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar-container {
  position: relative;
  margin-right: 15px;
}

.avatar {
  width: 70px;
  height: 70px;
  border-radius: 35px;
  border: 2px solid rgba(255, 255, 255, 0.5);
}

.level-tag {
  position: absolute;
  bottom: 0;
  right: 0;
  background-color: var(--secondary-color);
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
}

.user-details {
  flex: 1;
}

.username {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.user-id {
  font-size: 12px;
  opacity: 0.8;
  margin-bottom: 8px;
}

.edit-profile {
  display: inline-flex;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 4px 8px;
  border-radius: 15px;
  font-size: 12px;
}

.edit-profile .van-icon {
  margin-right: 4px;
}

/* 会员信息 */
.vip-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  padding: 12px 15px;
  margin: 15px 0;
}

.vip-info {
  display: flex;
  align-items: center;
}

.vip-tag {
  background-color: #FFD700;
  color: #8B4513;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  margin-right: 10px;
}

.vip-tag.active {
  background-color: #FFD700;
}

.vip-desc {
  font-size: 12px;
}

/* 资产信息 */
.assets {
  display: flex;
  margin-top: 15px;
}

.asset-item {
  flex: 1;
  text-align: center;
  position: relative;
}

.asset-item:not(:last-child)::after {
  content: '';
  position: absolute;
  right: 0;
  top: 20%;
  height: 60%;
  width: 1px;
  background-color: rgba(255, 255, 255, 0.3);
}

.asset-value {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.asset-label {
  font-size: 12px;
  opacity: 0.8;
}

/* 卡片样式 */
.card {
  background-color: white;
  border-radius: 10px;
  margin: 15px;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.card-title {
  font-size: 16px;
  font-weight: bold;
}

.card-more {
  display: flex;
  align-items: center;
  color: #999;
  font-size: 12px;
}

.card-more .van-icon {
  margin-left: 5px;
}

/* 订单图标 */
.order-icons {
  display: flex;
  justify-content: space-around;
}

.order-icon-item {
  text-align: center;
}

.icon-wrapper {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  background-color: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto 5px;
}

.icon-wrapper .van-icon {
  font-size: 20px;
  color: var(--primary-color);
}

.icon-text {
  font-size: 12px;
}

/* 服务网格 */
.service-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px 0;
}

.service-item {
  text-align: center;
}

.service-icon {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  background-color: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto 5px;
}

.service-icon .van-icon {
  font-size: 20px;
  color: var(--primary-color);
}

.badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #FF3B30;
  color: white;
  font-size: 10px;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.service-text {
  font-size: 12px;
}

/* 退出登录按钮 */
.logout-btn {
  margin: 20px 15px;
  padding: 12px 0;
  background-color: white;
  border-radius: 10px;
  text-align: center;
  color: #FF3B30;
  font-size: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
</style>